<?php
class User{
	private $setter;
	private $dbOperator;
	
	private $cedula;
	private $nombre;
	private $apellido;
	private $nick;
	private $password;
	private $rol;
	private $telefono;
	private $direccion;
	private $ciudad;
	private $pais;
	private $email;
	
	function User(){
		$this->setter=new Setter("../../Config");
		$this->dbOperator=new DbOperator($this->setter->getDataBaseHost(),$this->setter->getDataBaseUserName(),$this->setter->getDataBaseName(),$this->setter->getDataBasePassword());
		
		$this->cedula="";
		$this->nombre="";
		$this->apellido="";
		$this->nick="";
		$this->password="";
		$this->rol="";
		$this->telefono="";
		$this->direccion="";
		$this->ciudad="";
		$this->pais="";
		$this->email="";
		$this->rol="";
	}
	
	function setCedula($cedula=""){
		$this->cedula=$cedula;
	}
	function getCedula(){
		return $this->cedula;
	}
	
	function setNombre($nombre=""){
		$this->nombre=$nombre;
	}
	function getNombre(){
		return $this->nombre;
	}
	
	function setApellido($apellido=""){
		$this->apellido=$apellido;
	}
	function getApellido(){
		return $this->apellido;
	}
	
	function setNick($nick=""){
		$this->nick=$nick;
	}
	function getNick(){
		return $this->nick;
	}
	
	function setPassword($password=""){
		$this->password=$password;
	}
	function getPassword(){
		return $this->password;
	}
	function setRol($rol=""){
		$this->rol=$rol;
	}
	function getRol(){
		return $this->rol;
	}
	function setTelefono($telefono=""){
		$this->telefono=$telefono;
	}
	function getTelefono(){
		return $this->telefono;
	}
	
	function setDireccion($direccion=""){
		$this->direccion=$direccion;
	}
	function getDireccion(){
		return $this->direccion;
	}
	
	function setCiudad($ciudad=""){
		$this->ciudad=$ciudad;
	}
	function getCiudad(){
		return $this->ciudad;
	}
	
	function setPais($pais=""){
		$this->pais=$pais;
	}
	function getPais(){
		return $this->pais;
	}
	
	function setEmail($email=""){
		$this->email=$email;
	}
	function getEmail(){
		return $this->email;
	}
	
	function guardar(){
		$this->dbOperator->connect();
		$rolElegido=$this->rol;
		$rolId=$this->dbOperator->consult("SELECT id FROM roles WHERE nombre='$rolElegido'","yes");
		
		$this->dbOperator->dbInsert(array($this->cedula,$rolId[0],$this->nombre,$this->apellido,$this->nick,$this->password,$this->telefono,$this->direccion,$this->ciudad,$this->pais,$this->email),"usuarios");		
	}
	function eliminar(){
		$this->dbOperator->connect();
		$this->dbOperator->erase("usuarios","id",$this->cedula);			
	}
 	function actualizar(){
 		$this->dbOperator->connect();
 		
 		//Hallar id del rol del usuario
 		$rol=$this->dbOperator->consult("SELECT id FROM roles WHERE nombre='".$this->rol."'","yes");
 		$rolId=$rol[0];
 		//-------------------------------
 		$this->dbOperator->dbUpdate(array("id"=>$this->cedula,"idRol"=>$rolId,"nombre"=>$this->nombre,"apellido"=>$this->apellido,"nick"=>$this->nick,"password"=>$this->password,"telefono"=>$this->telefono,"direccion"=>$this->direccion,"ciudad"=>$this->ciudad,"pais"=>$this->pais,"email"=>$this->email),"usuarios","id",$this->cedula);
 	}
	function cargar($idUser="0"){
		$this->dbOperator->connect();
		$dataUser=$this->dbOperator->consult("SELECT * FROM usuarios WHERE id='".$idUser."'","yes");
		
		$this->cedula=$dataUser[0];
		
		//Hallar nombre del rol
		$rol=$this->dbOperator->consult("SELECT nombre FROM roles WHERE id='".$dataUser[1]."'","yes");
		//---------------------
		$this->rol=$rol[0];
		$this->nombre=$dataUser[2];
		$this->apellido=$dataUser[3];
		$this->nick=$dataUser[4];
		$this->password=$dataUser[5];
		$this->telefono=$dataUser[6];
		$this->direccion=$dataUser[7];
		$this->ciudad=$dataUser[8];
		$this->pais=$dataUser[9];
		$this->email=$dataUser[10];
	
	}
}
?>
